package com.weetop.akly.presenter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.luck.picture.lib.entity.LocalMedia;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.base.mvp.BaseObserver;
import com.weetop.akly.base.mvp.BasePresenter;
import com.weetop.akly.bean.ApproveFromKeyBean;
import com.weetop.akly.bean.UploadFileBean;
import com.weetop.akly.bean.VacationLogBean;
import com.weetop.akly.utils.BitmapUtil;
import com.weetop.akly.view.WorkApplyView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class WorkApplyPresent extends BasePresenter<WorkApplyView> {

    private int fileSize;
    private ArrayList<String> picList;

    public WorkApplyPresent(WorkApplyView baseView, Activity activity) {
        super(baseView, activity);
    }

    /**
     * 获取审批单表单选项
     */
    public void getApproveFormKeyList(String model_id) {
        addDisposable(apiServer.getApproveFormKeyList(model_id), new BaseObserver<BaseModel<ArrayList<ApproveFromKeyBean>>>(baseView) {
            @Override
            public void onSuccess(BaseModel<ArrayList<ApproveFromKeyBean>> o) {
                baseView.getApproveFormKeySuccess(o);
            }

            @Override
            public void onError(String msg) {
                if (baseView != null) {
                    baseView.showError(msg);
                }
            }
        });
    }

    /**
     * 上传文件
     */
    @SuppressLint("StaticFieldLeak")
    public void uploadFile(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            baseView.uploadSucess("");
            return;
        }
        fileSize = 1;
        picList = new ArrayList<>();
        new AsyncTask<Integer, Integer, File>() {
            @Override
            protected File doInBackground(Integer... params) {
                //压缩图片
                File file = null;
                try {
                    String pathname = BitmapUtil.bitmapToPath(filePath);
                    file = new File(pathname);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return file;
            }

            @Override
            protected void onPostExecute(File file) {
                super.onPostExecute(file);
                uploadFile(file);
            }
        }.execute();
    }

    private void uploadFile(File file) {
        RequestBody fileRQ = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), fileRQ);
        RequestBody fb = RequestBody.create(MediaType.parse("text/plain"), "pps");
        addDisposable(apiServer.uploadFile("http://file.auko.zjteam.com/upload.php",fb, part), new BaseObserver<BaseModel<UploadFileBean>>(baseView) {
            @Override
            public void onSuccess(BaseModel<UploadFileBean> o) {
                picList.add(o.getData().getFilePath());
                if (picList.size() == fileSize) {
                    String filePaths = "";
                    for (String filePath : picList) {
                        if (TextUtils.isEmpty(filePaths)) {
                            filePaths = filePath;
                        } else {
                            filePaths = filePaths + "," + filePath;
                        }
                    }
                    baseView.uploadSucess(filePaths);
                }
            }

            @Override
            public void onError(String msg) {
                if (baseView != null) {
                    baseView.showError(msg);
                }
            }
        });
    }

    /**
     * 上传多个文件
     */
    @SuppressLint("StaticFieldLeak")
    public void uploadFiles(ArrayList<String> filePaths) {
        if (filePaths.size() <= 0) {
            baseView.uploadSucess("");
            return;
        }
        fileSize = filePaths.size();
        picList = new ArrayList<>();
        new AsyncTask<Integer, Integer, List<File>>() {
            @Override
            protected List<File> doInBackground(Integer... params) {
                //压缩图片
                final List<File> files = new ArrayList<>();
                for (String path : filePaths) {
                    //压缩图片
                    File file = null;
                    try {
                        String pathname = BitmapUtil.bitmapToPath(path);
                        file = new File(pathname);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    files.add(file);
                }
                return files;
            }

            @Override
            protected void onPostExecute(List<File> files) {
                super.onPostExecute(files);
                for (File file : files) {
                    uploadFile(file);
                }
            }
        }.execute();

    }

    public void uploadFiles(List<LocalMedia> list) {
        if (list.size() <= 0) {
            baseView.uploadSucess("");
            return;
        }
        ArrayList<String> filePaths = new ArrayList<>();
        for (LocalMedia media : list) {
            filePaths.add(media.getPath());
        }
        uploadFiles(filePaths);
    }

    /**
     * 提交表单
     *
     * @param model_id
     * @param form
     */
    public void submitForm(String model_id, String leader_ids, HashMap<String, String> form) {
        Log.e("WorkOutSignPresent", "submitForm: " + GsonUtils.toJson(form));
        addDisposable(apiServer.submitForm(model_id, leader_ids, form), new BaseObserver<BaseModel<Object>>(baseView) {
            @Override
            public void onSuccess(BaseModel<Object> o) {
                baseView.submitSuccess(o);
            }

            @Override
            public void onError(String msg) {
                if (baseView != null) {
                    baseView.showError(msg);
                }
            }
        });
    }

    /**
     * 假期使用记录
     *
     * @param form
     */
    public void vacationLogList(HashMap<String, String> form) {
        Log.e("WorkOutSignPresent", "submitForm: " + GsonUtils.toJson(form));
        addDisposable(apiServer.vacationLogList( form), new BaseObserver<BaseModel<VacationLogBean>>(baseView) {
            @Override
            public void onSuccess(BaseModel<VacationLogBean> o) {
                LogUtils.e(o.getData());
                baseView.vacationLogListSuccess(o);
//                baseView.submitSuccess(o);
            }

            @Override
            public void onError(String msg) {
                if (baseView != null) {
                    baseView.showError(msg);
                }
            }
        });
    }


    /**
     * 加班
     *
     * @param model_id
     * @param form
     */
    public void overTimeAdd(String model_id, String leader_ids, HashMap<String, String> form) {
        Log.e("WorkOutSignPresent", "submitForm: " + GsonUtils.toJson(form));
        addDisposable(apiServer.overTimeAdd(model_id, leader_ids, form), new BaseObserver<BaseModel<Object>>(baseView) {
            @Override
            public void onSuccess(BaseModel<Object> o) {
                baseView.submitSuccess(o);
            }

            @Override
            public void onError(String msg) {
                if (baseView != null) {
                    baseView.showError(msg);
                }
            }
        });
    }
    /**
     * 外勤
     *
     * @param model_id
     * @param form
     */
    public void wqAdd(String model_id, String leader_ids, HashMap<String, String> form) {
        Log.e("WorkOutSignPresent", "submitForm: " + GsonUtils.toJson(form));
        addDisposable(apiServer.wqAdd(model_id, leader_ids, form), new BaseObserver<BaseModel<Object>>(baseView) {
            @Override
            public void onSuccess(BaseModel<Object> o) {
                baseView.submitSuccess(o);
            }

            @Override
            public void onError(String msg) {
                if (baseView != null) {
                    baseView.showError(msg);
                }
            }
        });
    }
    /**
     * 补卡
     *java.lang.IllegalArgumentException: Field map contained null value for key 'form[1][form_val]'.
     * @param
     * @param form
     */
    public void cardAdd(HashMap<String, String> form) {
        Log.e("WorkOutSignPresent", "submitForm: " + GsonUtils.toJson(form));
        addDisposable(apiServer.cardAdd(form), new BaseObserver<BaseModel<Object>>(baseView) {
            @Override
            public void onSuccess(BaseModel<Object> o) {
                baseView.submitSuccess(o);
            }

            @Override
            public void onError(String msg) {
                if (baseView != null) {
                    baseView.showError(msg);
                }
            }
        });
    }
    /**
     * 补卡
     *java.lang.IllegalArgumentException: Field map contained null value for key 'form[1][form_val]'.
     * @param
     * @param form
     */
    public void goOutAdd(HashMap<String, String> form) {
        Log.e("WorkOutSignPresent", "submitForm: " + GsonUtils.toJson(form));
        addDisposable(apiServer.goOutAdd(form), new BaseObserver<BaseModel<Object>>(baseView) {
            @Override
            public void onSuccess(BaseModel<Object> o) {
                baseView.submitSuccess(o);
            }

            @Override
            public void onError(String msg) {
                if (baseView != null) {
                    baseView.showError(msg);
                }
            }
        });
    }
}
